In [27]:
from keras.models import Sequential
from keras.layers import Dense
import numpy as np
import math
%matplotlib inline
import matplotlib.pyplot as plt
model = Sequential()
model.add(Dense(10, input_dim=2, init='uniform',activation='linear'))
model.add(Dense(10, activation='relu'))
model.add(Dense(5, activation='linear'))
model.add(Dense(1, activation='linear'))
model.compile(loss='mean_squared_error',optimizer='rmsprop',metrics=['accuracy'])
x_train = 2*np.random.random((2000,2))-1
#y_train = x_train[:,0:4].dot(x_train[:,4:8])
y_train = np.array([x_train[i,0]*x_train[i,1] for i in range(len(x_train))])
hist = model.fit(x_train,y_train, nb_epoch=100, batch_size=20,verbose=0,shuffle=True, validation_split=0.2)
In [28]:
x_val = 2*np.random.random((100,2))-1
y_val = np.array([x_val[i,0]*x_val[i,1] for i in range(len(x_val))])
out = model.predict(x_val)
errors= np.mean(np.abs(y_val-out),axis=1)
#plt.scatter(range(len(errors)),errors,c='r',label='Accurate')
#plt.legend(loc=1)
plt.hist(errors)
plt.show()
In [ ]: